package cn.signUpSystem.service.impl;

import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

import cn.signUpSystem.service.BaseService;

@SuppressWarnings("unchecked")
@Transactional
public class BaseServiceImpl<T> implements BaseService<T> {

	@Resource
	protected HibernateTemplate hibernateTemplate;

	public Class<T> clazz = null;

	public BaseServiceImpl() {
		ParameterizedType type = (ParameterizedType) this.getClass()
				.getGenericSuperclass();
		clazz = (Class<T>) type.getActualTypeArguments()[0];
	}

	public void add(T t) {
		hibernateTemplate.save(t);
	}

	public void update(T t) {
		hibernateTemplate.update(t);
	}

	public T queryOne(int id) {
		return hibernateTemplate.load(clazz, id);
	}

	public List<T> queryAll() {
		if (hibernateTemplate.find("FROM " + clazz.getSimpleName()).isEmpty()) {
			return null;
		}
		return hibernateTemplate.find("FROM " + clazz.getSimpleName());
	}

	public void delete(int id) {
		Object object = hibernateTemplate.load(clazz, id);
		if (object != null) {
			hibernateTemplate.delete(object);
		}
	}

}
